package com.mnt.washio.utils.network.ws;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;

import com.mnt.washio.objects.DateExtraObject;
import com.mnt.washio.objects.DateObject;
import com.mnt.washio.objects.TimeObject;
import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.NetworkUtils;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;

@SuppressLint("SimpleDateFormat")
public class GetDateOfOrderWSControl extends BaseWSControl {
	public String zipCode;

	public GetDateOfOrderWSControl(Context context,
			WebServiceCommunicatorListener listener, String zipCode) {
		super(context, listener, WebServiceFlag.GET_DATE);
		this.zipCode = zipCode;
	}

	@Override
	protected Object doInBackground(Void... params) {
		// Get local database

		String url = Statics.WASHIO_WS_URL_NEW + "orders/times/byzip/" + zipCode;

		try {
			String respone = NetworkUtils.doRequest(url);
			try {
				JSONObject jsonObject = new JSONObject(respone);

				return getDateFromJSONArray(jsonObject.getJSONArray("days"));
			} catch (JSONException e) {
				mError = e.getMessage();
			}

		} catch (ClientProtocolException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		} catch (IOException e) {
			mError = "Cannot connect to network";
			e.printStackTrace();
		}
		return null;
	}

	public ArrayList<DateExtraObject> getDateFromJSONArray(JSONArray proJSonArray)
			throws JSONException {
		ArrayList<DateExtraObject> arrayDateExtra = new ArrayList<DateExtraObject>();
		DateExtraObject dateExtraObject;
		JSONArray jsonArray = proJSonArray;
		JSONObject jsonObject;
		for (int i = 0; i < jsonArray.length(); i++) {
			jsonObject = jsonArray.getJSONObject(i);
			dateExtraObject = new DateExtraObject();
			dateExtraObject.setDate(jsonObject.getString("day"));
			dateExtraObject.setTime(getTimeObjectFromJson(jsonObject.getJSONArray("times")));
			dateExtraObject.setDropOffAfternoon(getDateObjectFromJson(jsonObject.getJSONArray("dropOffAfternoonDays")));
			dateExtraObject.setDropOffMorning(getDateObjectFromJson(jsonObject.getJSONArray("dropOffMorningDays")));
			arrayDateExtra.add(dateExtraObject);
		}
		return arrayDateExtra;
	}
	
	public ArrayList<DateObject> getDateObjectFromJson(JSONArray jsonArray){
		ArrayList<DateObject> myArray = new ArrayList<DateObject>();
		JSONArray jsonArrayRe = jsonArray;
		JSONObject jsonObj;
		DateObject dateObject;
		TimeObject time;
		for (int i = 0; i < jsonArrayRe.length(); i++) {
			try {
				jsonObj = jsonArrayRe.getJSONObject(i);
				dateObject = new DateObject();
				time = new TimeObject();
				dateObject.setDate(jsonObj.getString("day"));
				dateObject.setTimeObject(getTimeObjectFromJson(jsonObj.getJSONArray("times")));
				myArray.add(dateObject);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return myArray;
	}
	public ArrayList<TimeObject> getTimeObjectFromJson(JSONArray jsonArray){
		ArrayList<TimeObject> myArray = new ArrayList<TimeObject>();
		JSONArray jsonArrayRe = jsonArray;
		JSONObject jsonObj;
		TimeObject time;
		for (int i = 0; i < jsonArrayRe.length(); i++) {
			try {
				jsonObj = jsonArrayRe.getJSONObject(i);
				time = new TimeObject();
				time.setTime(jsonObj.getString("time"));
				time.setMorning(jsonObj.getBoolean("isMorning"));
				myArray.add(time);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return myArray;
	}
}
